package com.pig.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig.goods.dao.CategoryDao;
import com.pig.goods.dao.GoodsDao;
import com.pig.pojo.commgoods.Goods;
import com.pig.exception.DataException;
import com.pig.exception.NoDataFoundException;
import com.pig.exception.UnExceptDataException;
import com.pig.goods.service.GoodsService;
import com.pig.pojo.commgoods.PageObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private CategoryDao categoryDao;

    @Override
    public PageObject<Goods> findGoodsByTitle(String title, Integer status, Integer categoryId, Integer pageCurrent) {
        //1.检测pageCurrent是否正确
        if (pageCurrent == null || pageCurrent <= 0) {
            throw new IllegalArgumentException("页码不正确，请重试！");
        }
        //查询数据是否存在数据
        int rowCount = goodsDao.findGoodsCount(title,categoryId);
        if (rowCount == 0) {
            throw new NoDataFoundException("没有查询到相关记录！");
        }
        //2.查询goods集合
        List<Goods> goodsList = goodsDao.findGoodsByTitle(title, (pageCurrent-1)*pageSize, pageSize,status,categoryId);
        if (goodsList == null || goodsList.size() == 0) {
            throw new NoDataFoundException("查询失败！请重试");
        }
        return new PageObject<>(pageSize, rowCount, pageCurrent, goodsList);
    }

    @Override
    public PageObject findGoodsPages(String title,Integer pageCurrent) {
        int rowCount = goodsDao.findGoodsCount(title, -1);
        return new PageObject(5, rowCount, pageCurrent, null);
    }

    @Override
    public List<Map<String, Object>> findGoodsById(Integer goodsId) {
        //校验参数
        if (StringUtils.isEmpty(goodsId) || goodsId <= 0) {
            throw new IllegalArgumentException("参数不合法！");
        }
        //查询数据
        List<Map<String, Object>> goods = goodsDao.findGoodsById(goodsId);
        if (goods == null) {
            throw new DataException("查询结果异常！请重试!");
        }
        return goods;
    }

    /**
     * 更新/添加商品时校验商品的方法
     * @param goods
     * @return
     */
    private void checkGoodsData(Goods goods) {
        if (StringUtils.isEmpty(goods))
            throw new UnExceptDataException("发生异常！请重试");
        if (StringUtils.isEmpty(goods.getTitle()))
            throw new UnExceptDataException("商品名称不可为空！");
        if (StringUtils.isEmpty(goods.getCategoryId()))
            throw new UnExceptDataException("请选择商品所属目录！");
        if (StringUtils.isEmpty(goods.getDes()))
            throw new UnExceptDataException("商品描述不可为空！");
        if (StringUtils.isEmpty(goods.getPrice())||goods.getPrice() <= 0)
            throw new UnExceptDataException("商品价格错误！");
        if (StringUtils.isEmpty(goods.getOrigin()))
            throw new UnExceptDataException("商品产地不可为空！");
        if (StringUtils.isEmpty(goods.getWeight()))
            throw new UnExceptDataException("商品重量不可为空！");
        if (StringUtils.isEmpty(goods.getImg()))
            throw new UnExceptDataException("请上传商品图片！");
        if (StringUtils.isEmpty(goods.getStorage()))
            throw new UnExceptDataException("请填写商品保质期！");
        if (StringUtils.isEmpty(goods.getStorageFactor()))
            throw new UnExceptDataException("请填写存储条件！");
    }

    @Override
    public int updateGoods(Goods goods, Integer status) {
        if (StringUtils.isEmpty(goods.getId()) || goods.getId() <= 0)
            throw new UnExceptDataException("ID不可为空！");
        checkGoodsData(goods);
        goods.setStatus(status);
        goods.setUpdateTime(new Date());
        int row = goodsDao.updateById(goods);
        if (row == 0) {
            throw new DataException("添加失败！请重试");
        }
        return row;
    }

    @Override
    public int insertGoods(Goods goods) {
        goods.setId(null);
        checkGoodsData(goods);
        goods.setCreatedTime(new Date());
        goods.setUpdateTime(new Date());
        int row = goodsDao.insert(goods);
        return row;
    }

    @Override
    public int setGoodsStatus(Integer goodsId, Integer status) {
        if (goodsId == null || goodsId <= 0) {
            throw new UnExceptDataException("ID不正确，请重试！");
        }
        if (status != 0 && status != 1) {
            throw new UnExceptDataException("状态码不正确！");
        }
        int row = goodsDao.setGoodsStatus(goodsId, status);
        return row;
    }

    @Override
    public List<Goods> findGoodsByCategoryId(Integer categoryId) {
        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.eq("categoryId", categoryId);
        query.eq("status", 1);
        List<Goods> goods = goodsDao.selectList(query);
        return goods;
    }

    @Override
    public void delGoods(Integer goodsId) {
        goodsDao.deleteById(goodsId);
    }
}
